package com.bitcola.activity.biz;

import com.bitcola.activity.entity.RedPacketHelp;
import com.bitcola.activity.entity.RedPacketHelpLog;
import com.bitcola.activity.feign.IDataServiceFeign;
import com.bitcola.activity.mapper.RedPacketHelpLogMapper;
import com.bitcola.activity.mapper.RedPacketHelpMapper;
import com.bitcola.activity.mapper.SystemMapper;
import com.bitcola.exchange.security.common.exception.RepeatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author wx
 * @create 2018-11-29 21:00
 **/
@Service
public class RedPacketBiz {

    @Autowired
    RedPacketHelpMapper redPacketHelpMapper;

    @Autowired
    RedPacketHelpLogMapper redPacketHelpLogMapper;
    @Autowired
    IDataServiceFeign dataServiceFeign;


    @Transactional
    public RedPacketHelp apply(String userId) {

        Date now = new Date();
        String coinCode = dataServiceFeign.getConfig("red_packet_coin");
        BigDecimal initAmount = new BigDecimal(dataServiceFeign.getConfig("red_packet_init_amount"));
        BigDecimal finishAmount = new BigDecimal(dataServiceFeign.getConfig("red_packet_finish_amount"));
        long expireTime = ((now.getTime() + Long.parseLong(dataServiceFeign.getConfig("red_packet_expire"))) / 10000) * 10000;

        Example example = new Example(RedPacketHelp.class);
        example.createCriteria().andEqualTo("user_id", userId).andGreaterThan("expire_time", now);

        var list = redPacketHelpLogMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            throw new RepeatException("input repeat");
        }
        RedPacketHelp redPacketHelp = new RedPacketHelp();
        redPacketHelp.setId(UUID.randomUUID().toString());
        redPacketHelp.setCreateTime(now);
        redPacketHelp.setCoinCode(coinCode);
        redPacketHelp.setExpireTime(new Date(expireTime));
        redPacketHelp.setUserId(userId);

        redPacketHelp.setStatus("UNSHARE");
        redPacketHelp.setCurrentAmount(BigDecimal.ZERO);
        redPacketHelp.setIntiAmount(initAmount);
        redPacketHelp.setFinishAmount(finishAmount);
        redPacketHelpMapper.insert(redPacketHelp);
        return redPacketHelp;

    }

    @Transactional
    public int share(String redHelpId, String userId) {
        return         help(redHelpId,userId,userId,"share");


    }


    @Transactional
    public int help(String redHelpId,String userId,String initiatorId,String type ){
        int index=0;
        switch (type){
            case "share":{
                index = redPacketHelpMapper.updateStatus("SHARED", "UNSHARE", redHelpId);
                break;
            }
            case "help":
            case "kyc": {
                index = redPacketHelpMapper.updateStatus("SHARED", "SHARED", redHelpId);
                break;
            }
        }
        RedPacketHelp redPacketHelp = redPacketHelpMapper.selectByPrimaryKey(redHelpId);

        if (index == 0) throw new RepeatException("input repeat");

        BigDecimal originAmount = redPacketHelp.getCurrentAmount();
        BigDecimal newFoctor = caculateFactor(type, redPacketHelp.getFactor());
        BigDecimal currentAmount = new BigDecimal(sigMoid(newFoctor.doubleValue())).setScale(3);
        BigDecimal change = currentAmount.subtract(originAmount);
        if (currentAmount.compareTo(redPacketHelp.getFinishAmount()) >= 0) {
            currentAmount = redPacketHelp.getFinishAmount();
            redPacketHelpMapper.updateStatus("FINISHED", null, redHelpId);
        }

        redPacketHelpMapper.addFactorAndAmount(newFoctor, currentAmount, redHelpId);
        RedPacketHelpLog redPacketHelpLog = new RedPacketHelpLog();
        redPacketHelpLog.setId(UUID.randomUUID().toString());
        redPacketHelpLog.setAmount(change);
        redPacketHelpLog.setCreateTime(new Date());
        redPacketHelpLog.setRedPacketHelpId(redHelpId);
        redPacketHelpLog.setUserId(userId);
        redPacketHelpLog.setInitiatorId(initiatorId);
        redPacketHelpLog.setType(type);
        redPacketHelpLogMapper.insert(redPacketHelpLog);
        return 0;

    }


    public List<RedPacketHelpLog>search(String userId)
    {
        Example example = new Example(RedPacketHelpLog.class);
        example.createCriteria().andEqualTo("initiator_id", userId);

        example.orderBy("create_time").desc();
        var list = redPacketHelpLogMapper.selectByExample(example);
        return  list;


    }

    public static BigDecimal caculateFactor(String type, BigDecimal factor) {


        factor = factor.add(getFactor(type));


        return BigDecimal.valueOf(sigMoid(factor.doubleValue()));
    }

    public static BigDecimal getFactor(String type) {
        switch (type) {
            case "share": {
                return BigDecimal.valueOf(0.4);
            }
            case "help":{
                return BigDecimal.valueOf(0.05);
            }
            case "kyc":{

                return BigDecimal.valueOf(0.5);
            }

        }
        return BigDecimal.ZERO;
    }

    public static double sigMoid(double value) {
        double ey = Math.pow(Math.E, -value);
        double result = 1 / (1 + ey);
        return result*6.1;
    }


}
